package net.sf.cldsql.dj.utl;

import java.math.BigDecimal;
import java.util.List;

/**
 * JsonUtl
 */
public class JsonUtl {

    private List list;
    private String quotation;

    public JsonUtl (String quotation) {
        this.quotation = quotation;
    }
    
    public JsonUtl () {
        this("\"");
    }
    
    /**
     * @param params
     * @return
     */
    public JsonUtl set(List list) {
        this.list = list;
        return this;
    }
    
    public String toString() {
        StringBuilder json = new StringBuilder();
        json.append('[');
        boolean needComma = false;
        for (Object object : list) {
            if (needComma) {
                json.append(", ");
            }
            
            if (object == null) {
                json.append("null");                
            } else if (object instanceof CharSequence) {
                json.append(quotation + object.toString() + quotation);                
            } else if (object instanceof Short
                    || object instanceof Integer
                    || object instanceof Long
                    || object instanceof BigDecimal) {
                json.append(object.toString());
            } else {
                json.append(quotation + object.toString() + quotation);                
            }
            
            needComma |= true;
        }
        json.append(']');
        return json.toString();
    }

}
